package com.buer.Listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSON;
import com.buer.dao.dmo.ExcelOriginDataEntity;
import com.buer.service.dto.EachSheetDataDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by buer on 2019/10/14.
 */
public class ExcelOriginDataListener extends AnalysisEventListener<ExcelOriginDataEntity> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelOriginDataListener.class);

    public Map<Integer, EachSheetDataDto> originDataMap = new HashMap<Integer, EachSheetDataDto>();

    public void invoke(ExcelOriginDataEntity excelOriginDataEntity, AnalysisContext analysisContext) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(excelOriginDataEntity));
        Sheet currentSheet = analysisContext.getCurrentSheet();
        int sheetNo = currentSheet.getSheetNo();

        // 要是map中没有这个sheetNo的信息，就增加
        if (!originDataMap.containsKey(sheetNo)) {
            EachSheetDataDto eachSheetDataDto = new EachSheetDataDto();
            eachSheetDataDto.setSheetName(currentSheet.getSheetName());
            eachSheetDataDto.setSheetNo(sheetNo);
            originDataMap.put(sheetNo, eachSheetDataDto);
        }

        // 添加解析好dmo到sheet信息中
        originDataMap.get(sheetNo).getSheetOriginDataList().add(excelOriginDataEntity);
    }

    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
